package com.wx.activitys.drp.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wx.activitys.base.utils.Common;
import com.wx.activitys.drp.EnumUtils.EnumIncomeType;
import com.wx.activitys.drp.EnumUtils.EnumTream;
import com.wx.activitys.drp.mapper.BUserIncomeMapper;
import com.wx.activitys.drp.mapper.DictionaryMapper;
import com.wx.activitys.drp.mapper.ProfitMapper;
import com.wx.activitys.drp.mapper.ShareMapper;
import com.wx.activitys.drp.mapper.UserInfoMapper;
import com.wx.activitys.drp.model.BDictionary;
import com.wx.activitys.drp.model.BProfit;
import com.wx.activitys.drp.model.BShare;
import com.wx.activitys.drp.model.BUserIncome;
import com.wx.activitys.drp.model.BUserInfo;
import com.wx.activitys.drp.service.UserInfoService;

@Service("/userInfoService")
public class UserInfoServiceImpl implements UserInfoService {
    @Autowired
    UserInfoMapper userInfoMapper;
    @Autowired
    DictionaryMapper dictionaryMapper;
    @Autowired
    BUserIncomeMapper bUserIncomeMapper;
    @Autowired
    ShareMapper shareMapper;
    @Autowired
    ProfitMapper profitMapper;
	
	@Override
	public int insert(BUserInfo bUserInfo) {
		return userInfoMapper.insert(bUserInfo);
	}

	@Override
	public BUserInfo findByOpenid(String openid) {
		return userInfoMapper.findByOpenid(openid);
	}

	@Override
	public int update(Map<String, Object> map) {
		return userInfoMapper.update(map);
	}

	@Override
	public List<BUserInfo> selectByMap(Map<String, Object> map) {
		return userInfoMapper.selectByMap(map);
	}

	@Override
	public List<BUserInfo> findAll(BUserInfo bUserInfo) {
		return userInfoMapper.findAll(bUserInfo);
	}

	@Override
	public int countUserInfoByMap(BUserInfo bUserInfo) {
		return userInfoMapper.countUserInfoByMap(bUserInfo);
	}

    /**
     * 
     */
	@Override
	public int insertAndupdate(BUserInfo bUserInfo,String pId) {
		if(StringUtils.isNotBlank(pId)){
			bUserInfo.setRegisterStatus(0);
		}else{
			bUserInfo.setRegisterStatus(1);
		}
		int flag=userInfoMapper.insert(bUserInfo);
		/**
		 * 注册进来一个人初始化分销分销表的数据
		 */
		List<BShare> list= new ArrayList<>();
		if(bUserInfo.getTreamRole().equals(EnumTream.经销商.getValue())){
			List<BProfit> bProfits=profitMapper.findByRoleKey("1");
			for (BProfit bProfit : bProfits) {
					BShare bs = new BShare();
					bs.setPlatfromId(bProfit.getPaltformId());
					bs.setDefaultValue(bProfit.getValue());
					bs.setOpenid(bUserInfo.getOpenid());
					bs.setValue(0.0);
					list.add(bs);
				}
				shareMapper.addBatch(list);
			}else{
				List<BShare> bShares =  shareMapper.findByOpenid(pId);
				for (BShare bShare : bShares) {
					list.clear();
					BShare bs = new BShare();
					bs.setDefaultValue(bShare.getValue());
					bs.setPlatfromId(bShare.getPlatfromId());
					bs.setOpenid(bUserInfo.getOpenid());
					bs.setValue(0.0);
					list.add(bs);
				}
				shareMapper.addBatch(list);
			}
		
		/*if(StringUtils.isNotBlank(pId)){
			BDictionary entity = new BDictionary();
			Map<String,Object> map = new HashMap<>();
			entity.setCode("registered_share");
			BDictionary bi=dictionaryMapper.selectEntity(entity);
			if(StringUtils.isNotBlank(bi.getValue())){
				Double income = Double.valueOf(bi.getValue());
				map.put("incomeadd", income);
				map.put("openid", pId);
				flag=userInfoMapper.update(map);
				BUserIncome bu = new BUserIncome();
				bu.setId(Common.generateUUID());
				bu.setIncome(income);
				bu.setOpenid(pId);
				bu.setCreateTime(new Date());
				bu.setType(EnumIncomeType.注册.getValue());
				flag=bUserIncomeMapper.insert(bu);	
			}
		}*/
		return flag;
	}

	
	

}
